﻿CREATE PROCEDURE Perfiles_DevolverPorId

@IdPerfil INT

AS

SELECT p.IdPerfil,
	p.Nombre,
	p.Observacion
	FROM Perfiles p
	WHERE p.IdPerfil = @IdPerfil

SELECT pd.IdPerfilDetalle,
	pd.IdPerfil,
	pd.IdPermiso,
	CONVERT(BIT, 1) AS TienePermiso
	FROM PerfilesDetalles pd
			
UNION
				
SELECT 0 AS IdPerfilDetalle,
	p.IdPerfil,
	Pe.IdPermiso,
	CONVERT(BIT, 0) AS TienePermiso
	FROM Perfiles p
		CROSS JOIN Permisos pe
	WHERE CONVERT(VARCHAR, p.IdPerfil + '-' + pe.IdPermiso) NOT IN (SELECT CONVERT(VARCHAR, pd.IdPerfil + '-' + pd.IdPermiso)
																		FROM PerfilesDetalles pd)